<?php
require_once('table_model.php');

class Videos_model extends Table_model{
	var $relational_tables = array(
		'images_rel_videos'
	);
	
	function Videos_model(){
		parent::Table_model();
		$this->ci = &get_instance();
		
		$this->table = 'videos';
		$this->table_name = 'Videos';
		
		$this->id_field = 'video_id';
		$this->file_location_field = 'video_path';
		
		$this->fields = array(
			'video_id'					=> array(
												'name' 	=> 'Video ID',
												'view'	=> false,
												'edit'	=> array( 
															'func' 		=> 'form_hidden',
															'params'	=> null,
															'label'		=> false
														)
											),
			'video_title'				=> array(
												'name' 	=> 'Title',
												'view'	=> true,
												'sort'	=> true,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_input_wrapper'),
															'params'	=> array( array('id' => 'video_title') ),
															'label'		=> true
														),
												'rules'	=> 'required|trim'
											),
			'video_path'					=> array(
												'name' 	=> 'Path',
												'view'	=> false,
												'sort'	=> false,
												'edit'	=> false
											)
		);
	}
	
	function deleteItems($ids, $delete_relational = true){
		if( !is_array($ids)){
			$ids = array($ids);
		}
		$ids = array_filter($ids, 'is_numeric');
		if( count($ids)){
			$in = '(' . implode(',', $ids) . ')';
			$query = $this->db->query("SELECT * FROM {$this->table} WHERE {$this->id_field} IN $in");
			if( $query && $query->num_rows()){
				foreach( $query->result() as $row){
					$file = realpath($_SERVER['DOCUMENT_ROOT']) . $row->{$this->file_location_field};
					if( file_exists($file) ){
						unlink( $file);
					}
				}
			}
			parent::deleteItems( $ids );
			if( $delete_relational ){
				if( count($this->relational_tables)){
					$ci = &get_instance();
					$ci->load->model('RelatedVideos_model');
					foreach( $this->relational_tables as $table){
						$ci->RelatedVideos_model->setTable($table);
						$ci->RelatedVideos_model->deleteRelatedItemsById($ids);
					}
				}
			}
		}
	}
}
?>